﻿/**
 * This is a part of MCServe, which is available under the Apache license 2.0.
 * With this code, you may do whatever you want as long as that is not in conflict with this license.
 * In addition, you should have a reference to the original project to include (http://mcserve.codeplex.com).
 * 
 * Author       : Lars Krämer ( Xe77a )
 * Website      : http://www.lkraemer.de
 * Project      : MCServe (http://mcserve.codeplex.com)
 * Copyright    : Copyright (c) 2013 lkraemer.de
 * License      : http://mcserve.codeplex.com/license
 * Description  : This part contains the functions of MCServe API
 */

using MCServeLib;
using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;

namespace MCServeAPI
{
    public class ApiRequest
    {
        private ApiActions _ApiAction = ApiActions.NONE;
        private Dictionary<string, string> _RequestValues = new Dictionary<string, string>();
        private DownloadStringCompletedEventHandler _RequestHandle = null;

        public ApiRequest()
        {
        }

        public ApiActions ApiAction
        {
            set
            {
                _ApiAction = value;
            }
            get
            {
                return _ApiAction;
            }
        }
        public Dictionary<string, string> RequestValues
        {
            set
            {
                _RequestValues = value;
            }
            get
            {
                return _RequestValues;
            }
        }
        public DownloadStringCompletedEventHandler RequestHandle
        {
            set
            {
                _RequestHandle = value;
            }
            get
            {
                return _RequestHandle;
            }
        }

        public void AddValue(string title, string value)
        {
            RequestValues.Add(title, value);
        }
        public void Send()
        {
            string UrlValues = "";
            int i = 1;

            foreach(var RequestValue in RequestValues)
            {
                if (i == 1)
                {
                    UrlValues += "?";
                }
                else if (i > 1 && i <= RequestValues.Count)
                {
                    UrlValues += "&";
                }

                UrlValues += RequestValue.Key + "=" + RequestValue.Value;

                i++;
            }

            System.Uri uri = new Uri(MCServeSites.API + "/Service/" + ApiAction + UrlValues);

            WebClient webClient = new WebClient();
            webClient.Headers["user-agent"] = "MCServe App";
            webClient.Headers["user-platform"] = "Windows " + Environment.OSVersion.Version.Major + "." + Environment.OSVersion.Version.Minor + " (Build " + Environment.OSVersion.Version.Build + ")";
            webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(RequestHandle);
            webClient.DownloadStringAsync(uri);
        }
    }
}
